package finder;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import applicationDAO.InstituteDAO;

import com.project.trac.R;

import core.modules.DatabaseConnector;

public class SubjectViewer extends InstituteDAO {

	
	public static ListFragment newInstance(Context context) {
		SubjectViewer lf = new SubjectViewer();
		return lf;
	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		ViewGroup root = (ViewGroup) inflater.inflate(R.layout.generic_list_a, null);
		
		new AsynchronousTask().execute();
		return root;
	}
	
	public void onAttachedToWindow() {
		super.getActivity().onAttachedToWindow();
		Window window = getActivity().getWindow();
		window.setFormat(PixelFormat.RGBA_8888);
	}	
	
	private class AsynchronousTask extends AsyncTask<String, String, String> {

		HashMap<String, String> m;
		ArrayList<HashMap<String, String>> arrayList = new ArrayList<HashMap<String, String>>();

		ListAdapter listAdapter;
		
		String KEY_CAT = "k1";
		String KEY_SECTION = "k2";
		String KEY_DAYS = "k3";
		String KEY_TIME = "k4";
		String KEY_ROOM = "k5";
		String KEY_INSTRUCTOR = "k6";
		
		String CATALOG_NUMBER = "v1";
		String VALUE_INSTRUCTOR = "v2";
		
		String query;
		
		int rowCount;

		ResultSet rs;
		
		protected void onPreExecute() {
			super.onPreExecute();
		}

		protected String doInBackground(String... args) {
			query = "SELECT curriculum.catalognumber, schedule.section, schedule.days, schedule.time, "
					+ " schedule.room, users.title, users.firstname, users.lastname "
					+ " FROM gradesheet"
					+ " INNER JOIN schedule ON schedule.catalognumber = gradesheet.catalognumber "
					+ " INNER JOIN users ON users.id = schedule.creator"
					+ " INNER JOIN curriculum ON curriculum.id = gradesheet.catalognumber"
					+ " WHERE gradesheet.uid = " + getUserID();
			try {
					rs = DatabaseConnector.getStatement().executeQuery(query);

					while (rs.next()) {
						m = new HashMap<String, String>();
						
						CATALOG_NUMBER = rs.getString("catalognumber");
						VALUE_INSTRUCTOR = rs.getString("title") + " " + rs.getString("firstname")  
									   + " " + rs.getString("lastname");
						
						m.put(KEY_CAT, CATALOG_NUMBER);
						m.put(KEY_SECTION, rs.getString("section"));
						m.put(KEY_DAYS, rs.getString("days"));
						m.put(KEY_TIME, rs.getString("time"));
						m.put(KEY_ROOM, rs.getString("room"));
						m.put(KEY_INSTRUCTOR, VALUE_INSTRUCTOR);
						
						arrayList.add(m);
					}
					
					rowCount = rs.last() ? rs.getRow() : 0;
					
			} catch (SQLException e) {
				Log(getClass(), "doInBackground", e);
			}

			return null;
		}


		protected void onPostExecute(String postExecute) {
			
			TextView msg = new TextView(getActivity());
			
			if (rowCount == 0) {
				msg.setText("No enrolled subjects found.");
			} else if (rowCount == 1) {
				msg.setText("You are already enrolled\nin " + rowCount + " Subject!");
			} else if (rowCount > 1) {
				msg.setText("You are already enrolled\nin " + rowCount + " Subjects!");
			}
			msg.setTextAppearance(getActivity(), R.style.alert_message);
			msg.setGravity(Gravity.CENTER);
			Toast.makeText(getActivity().getApplicationContext(), msg.getText().toString(), Toast.LENGTH_SHORT).show();
			
			getActivity().runOnUiThread(new Runnable() {

				@Override
				public void run() {
					listAdapter = new SimpleAdapter(getActivity(), arrayList,
							R.layout.detail_result_course_enrolled, 
							new String[] { KEY_CAT, KEY_SECTION,  KEY_DAYS,  KEY_TIME,  KEY_ROOM,  KEY_INSTRUCTOR }, 
							new int[] { R.id.r_key_cat_new, R.id.actualSection,  R.id.actualDay,  R.id.actualTime,  R.id.actualRoom,  R.id.actualInstructor});

					setListAdapter(listAdapter);
				}
			});
		}
	} 
	
}